﻿
using EFCoreToApi.Models.Model;
using Microsoft.EntityFrameworkCore;
using System.Linq;

namespace EFCoreToApi.Models
{
    public class MyDbContext:DbContext
    {
        

        public MyDbContext(DbContextOptions<MyDbContext> options):base(options)
        {

        }
        protected override void OnModelCreating(ModelBuilder modelBuilder)
        {
            var Assembly = this.GetType().Assembly;
            var Types = Assembly.GetTypes().Where(m =>
                                                       m.FullName != null
                                                       && typeof(EntityBase<int>).IsAssignableFrom(m)
                                                       && !m.IsAbstract
                                                       && m.Name != "EntityBase").ToArray(); ;
            foreach (var entityType in Types)
            {
                modelBuilder.Entity(entityType);
            }
            modelBuilder.ApplyConfigurationsFromAssembly(Assembly);
            base.OnModelCreating(modelBuilder);
        }
    }
}
